﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using Contracts;
using System.ServiceModel.Channels;

namespace Client
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            NetTcpBinding bind = new NetTcpBinding();
            EndpointAddress address = new EndpointAddress("net.tcp://127.0.0.1:9999/calculatorservice");
            CreateClientInstanceByChannel(bind, address);
        }

        /// <summary>
        /// 采用通道工厂的方式生成客户端服务对象实例
        /// </summary>
        /// <param name="bind"></param>
        /// <param name="address"></param>
        private static void CreateClientInstanceByChannel(Binding bind, EndpointAddress address)
        {
            ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>(bind);
            var channel = channelFactory.CreateChannel(address);
            using (channel as IDisposable)
            {
                CallService(channel.Subtract);
                CallService(channel.Multiply);
                CallService(channel.Divide);
            }
        }

        private static void CallServiceByCode()
        {
            using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>("calculatorservice"))
            {
                ICalculator proxy = channelFactory.CreateChannel();
                using (proxy as IDisposable)
                {
                    CallService(proxy.Subtract);
                    CallService(proxy.Multiply);
                    CallService(proxy.Divide);
                }
            }
        }

        private static void CallService(Func<double, double, double> serviceMethod)
        {
            Console.Write("请输入第1个数字：");
            double input1 = double.Parse(Console.ReadLine());
            Console.Write("请输入第2个数字:");
            double input2 = double.Parse(Console.ReadLine());
            double result = serviceMethod(input1, input2);
            string methodName = serviceMethod.Method.Name;
            Console.Write("两个数字{0}结果：{1}\n", methodName, result);
        }
    }
}